﻿using HCMS.CommonDtos;
using HCMS.Entities.Manage;
using HCMS.IRepositories.Manage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;

namespace HCMS.Repositories.Manage
{
    /// <summary>
    /// 管理员 仓储层
    /// </summary>
    public class AdminRepository : ManageBaseRepository<Admin>, IAdminRepository
    {
        public AdminRepository(IDbContextProvider<ManageDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<Admin>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var query = await GetQueryableAsync();

            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                switch (searchArgs.SearchName)
                {
                    case "name":
                        query = query.Where(t => t.AdminName.Contains(searchArgs.SearchKey));
                        break;
                }
            }

            if (searchArgs.Status != 9999)
            {
                query = query.Where(t => t.Status == searchArgs.Status);
            }

            var count = await query.CountAsync();
            pager.RowCount = count;

            var list = new List<Admin>();

            if (count > 0)
            {
                query = query.OrderByDescending(t => t.Id);

                list = await query
                           .Skip((pager.CurrentPageIndex - 1) * pager.PageSize)
                           .Take(pager.PageSize)
                           .ToListAsync();
            }

            var result = new ListPagerDto<Admin>()
            {
                Data = list ?? new List<Admin>(),
                PagerInfo = pager
            };

            return result;
        }

        /// <summary>
        /// 获取全部管理员
        /// </summary>
        /// <returns></returns>
        public async Task<List<Admin>> AllList()
        {
            var query = await GetQueryableAsync();
            var result = query?.ToList();
            return result ?? new List<Admin>();
        }

        /// <summary>
        /// 判断管理员是否存在
        /// </summary>
        /// <param name="adminName">管理员 名称</param>
        /// <returns></returns>
        public async Task<bool> IsExist(string adminName)
        {
            // 1、通过 EF 直接查询
            //var a = GetAsync(1).Result;
            //if (a == null) { return false; }

            // 2、通过 sql 语句查询
            var id = 1;
            FormattableString sql = $"SELECT COUNT(1) FROM admin WHERE id = {id}";
            var b = GetDbContextAsync().Result.Database.SqlQuery<int>(sql).ToList()[0];

            return true;
        }
    }
}
